﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Collections;
using CustomMVC;
using Ext.Net;
using Ext.Net.MVC;
using System.Configuration;
using System.Text;
using EFMysql;
using BLL;
using CommonLib;
using System.Diagnostics;

namespace ShuaDanMysql.Controllers
{
    [DirectController]
    public class MainController : BaseController
    {
        //
        // GET: /Main/
        UserBll userBll = new UserBll();
        SysBll sysBll=new SysBll();

        public ActionResult Index()
        {
           
            return View();
        }        

        public ActionResult Content()
        {

            return View();
        }

        public ActionResult Menu()
        {
            List<pre_menu> menu = sysBll.GetMenus().ToList();
            ItemCollection itemsCollection = new ItemCollection();
            List<pre_privileges> privileges = GetFuncPrivilege(Session.SessionID);
            foreach (pre_menu item in menu.FindAll(o => o.ParentCode == "Root0001"))
            {
                if (privileges.FindAll(o => o.FUNCID == item.Code).Count > 0)
                {
                    TreePanel subMenu = new TreePanel();
                    subMenu.Title = item.Title;
                    subMenu.RootVisible = false;
                    subMenu.ID = item.Code;
                    subMenu.Root.Add(GetMenuList(menu, item.Code));
                    itemsCollection.Add(subMenu);
                }
            }
            return View(itemsCollection);          
        }

        [ErrorOper]
        public JsonResult Logout()
        {
            userBll.Logout(Session.SessionID);
            Hashtable ht = new Hashtable();
            ht.Add("Result", "OK");
            return Json(ht);
        }

        [ErrorOper]
        public ActionResult Header()
        {
            pre_user user = userBll.GetUserBySessionID(Session.SessionID);
            ViewData["Privileges"] = GetFuncPrivilege(Session.SessionID);
            ViewData["userNickName"] = user.username;
            ViewData["Timer"] = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") ;
            return View();
        }

        [ErrorOper]
        public JsonResult LoginHeartBeat()
        {
            if (userBll.CheckLogin(Session.SessionID))
            {
                Hashtable ht = new Hashtable();
                ht.Add("Result", "OK");
                return Json(ht);
            }
            else
            {
                Hashtable ht = new Hashtable();
                ht.Add("Result", "FAIL");
                return Json(ht);
            }
        }

        [DirectMethod]
        [HttpPost, OpLog("修改密码"), ErrorOper]
        public ActionResult ModifyPassword(string oldPasswordInput, string newPasswordInput, string repeatPasswordInput)
        {
            string newPassword = newPasswordInput;
            string oldPassword = oldPasswordInput;
            pre_user user = userBll.GetUserBySessionID(Session.SessionID);
            Hashtable ht = new Hashtable();
            if (userBll.ModPassword(Session.SessionID, GeneralTools.MD5Encrpyt(ConfigurationManager.AppSettings["passsalt"].ToString() + newPassword), 
                GeneralTools.MD5Encrpyt(ConfigurationManager.AppSettings["passsalt"].ToString() + oldPassword)))
            {
                Trace.Write(DateTime.Now+" 用户名： " + user.username + "修改密码成功！\n\r");
                ht.Add("Result", "OK");

            }
            else
            {
                ht.Add("Result", "FAIL");
                Trace.Write("用户名：" + user.username + "修改密码失败，原因：旧密码错误！\n\r");
                ht.Add("Message", "旧密码错误");
            }
            return this.Direct(ht);
        }

        private Node GetMenuList(List<pre_menu> menuList, string parentID)
        {
            List<pre_privileges> privileges = GetFuncPrivilege(Session.SessionID);
            Node root = new Node();
            root.Text = "root";
            root.Expanded = true;
            foreach (pre_menu item in menuList.FindAll(o => o.ParentCode == parentID))
            {
                if (privileges.FindAll(o => o.FUNCID == item.Code).Count > 0)
                {
                    Node subNode = new Node();
                    subNode.Text = item.Title;
                    if (item.IsLeaf == true)
                    {
                        subNode.NodeID = item.Code;
                        subNode.Href = "javascript:AddTab('" + item.Title + "','tab" + item.Code + "','" + item.Url +
                                       "',this);";
                        subNode.Leaf = true;
                    }
                    else
                    {
                        subNode.Leaf = false;
                        subNode.NodeID = item.Code;
                        subNode.Children.Add(GetSubMenuList(subNode, menuList, item.Code));
                    }
                    root.Children.Add(subNode);
                }
            }
            return root;
        }

        private Node GetSubMenuList(Node parentNode, List<pre_menu> menuList, string parentID)
        {
            List<pre_privileges> privileges = GetFuncPrivilege(Session.SessionID);
            foreach (pre_menu item in menuList.FindAll(o => o.ParentCode == parentID))
            {
                if (privileges.FindAll(o => o.FUNCID == item.Code).Count > 0)
                {
                    Node subNode = new Node();
                    subNode.Text = item.Title;
                    if (item.IsLeaf == true)
                    {
                        subNode.Leaf = true;
                        subNode.NodeID = item.Code;
                        subNode.Href = "javascript:AddTab('" + item.Title + "','tab" + item.Code + "','" + item.Url +
                                       "',this);";
                    }
                    else
                    {
                        subNode.Leaf = false;
                        subNode.NodeID = item.Code;
                        subNode.Children.Add(GetSubMenuList(subNode, menuList, item.Code));
                    }
                    parentNode.Children.Add(subNode);
                }
            }
            return parentNode;
        }
    }
}
